package com.example.dell.basicapplication;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.example.dell.adapter.FragmentChengAdapter;
import com.example.dell.basic.BasicBasicActivity;
import com.example.dell.fragment.CartFragment;
import com.example.dell.fragment.HomeFragmet;
import com.example.dell.fragment.SelfFragment;
import com.example.dell.fragment.ShopFragment;
import com.example.dell.utils.TabEntity;
import com.example.dell.utils.ToastUtils;
import com.example.dell.weigit.NoScrollViewPager;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

public class MainActivity extends BasicBasicActivity {

    private int[] mIconUnselectIds = {
            R.drawable.miji, R.drawable.no_active,
            R.drawable.no_listener, R.drawable.no_my};
    private int[] mIconSelectIds   = {
            R.drawable.no_miji, R.drawable.active,
            R.drawable.listener, R.drawable.my};

    private ArrayList<Fragment>  mFragments      = new ArrayList<>();
    private String               mFragmentTags[] = {"首页", "列表", "购物车", "我的"};
    private long                 mPreTime        = 0L;
    private NoScrollViewPager    mViewPager;
    private FragmentChengAdapter mAdapter;
    private CommonTabLayout      mCommonTabLayout;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected void initData() {
        //这个永远是成功界面
        autoShowPager(true, "");
    }

    @Override
    protected int setContentSuccessView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(View view) {
        mViewPager = (NoScrollViewPager) view.findViewById(R.id.no_scroll_viewpager);
        mCommonTabLayout = (CommonTabLayout) view.findViewById(R.id.commontablayout);
        mFragments.add(new HomeFragmet());
        mFragments.add(new ShopFragment());
        mFragments.add(new CartFragment());
        mFragments.add(new SelfFragment());

        //初始化选项绑定数据
        for (int i = 0; i < mFragmentTags.length; i++) {
            mTabEntities.add(new TabEntity(mFragmentTags[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mCommonTabLayout.setTabData(mTabEntities);


        mAdapter = new FragmentChengAdapter(getSupportFragmentManager(), mFragments, mFragmentTags);
        mViewPager.setAdapter(mAdapter);


        //默认滚动到零
        mCommonTabLayout.setCurrentTab(0);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                mCommonTabLayout.setCurrentTab(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        mCommonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                //是否开启动画。false为不开启，ture为开启动画
                mViewPager.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        getBasicBar().setVisibility(View.GONE);

    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - mPreTime > 2000) {// 两次点击间隔大于2秒
            ToastUtils.showToast("再按一次退出应用");
            mPreTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

}
